import os
from os.path import join

from aip import AipOcr
from configparser import ConfigParser

import setings
from utils import ViewModel


class Card(object):
    # def __init__(self):
    #     self.name = ''
    #     self.sex = ''
    #     self.nation = ''
    #     self.birthday = ''
    #     self.ID_numer = ''
    #     self.address = ''

    name = ''
    sex = ''
    nation = ''
    birthday = ''
    ID_numer = ''
    address = ''
    start = ''
    end = ''



class CardAip():

    def __init__(self, filePath):
        cp=ConfigParser()

        path = setings.BASE_DIR
        print(path)
        path = join(path, 'config.ini')
        cp.read(path)
        section = cp.sections()[0]
        APP_ID = cp.get(section,'APP_ID')
        API_KEY = cp.get(section,'API_KEY')
        SECRET_KEY = cp.get(section,'SECRET_KEY')

        # print(APP_ID)
        # print(API_KEY)
        # print(SECRET_KEY)
        self.client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
        self.filePath = filePath



    def getinfo(self,face):

        try:

            if face=='Front':
                with open(self.filePath, 'rb') as fp:
                    image = fp.read()
                idCardSide = "front"
                result = self.client.idcard(image, idCardSide)
                result = result['words_result']
                return result

            if face=='Back':
                with open(self.filePath, 'rb') as fp:
                    image = fp.read()
                idCardSide = "back"
                result = self.client.idcard(image, idCardSide)
                result = result['words_result']
                return result

        except:
            print("Error: 没有找到文件或读取文件失败")


if __name__ == '__main__':
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    print(BASE_DIR)
